package com.jkl.service.home;

import com.jkl.dao.home.SourceClassificationMapper;
import com.jkl.entity.home.HomeRequestEntity;
import com.jkl.entity.home.HomeSourceMappingEntity;
import com.jkl.entity.home.SourceResponseEnetity;
import com.jkl.service.home.sourceCassificationInter.SourceCassificationNewService;
import com.jkl.untils.DateUtil;
import com.jkl.untils.GetIntUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.math.BigDecimal;
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author zjj
 * @date  2019/9/2 15:44
 * @Description  ${描述一个类的相关信息}
 */
@Service
public class SourceCassificationNewServiceImpl implements SourceCassificationNewService {
    @Autowired
    private SourceClassificationMapper sourceClassificationDao;

    @Override
    public SourceResponseEnetity getCoverageTrend(HomeRequestEntity homeRequestEntity) {
        if (StringUtils.isEmpty(homeRequestEntity.getEndTime()) && StringUtils.isEmpty(homeRequestEntity.getStartTime())) {
            //获取最近一年开始时间和结束时间不包括本月
            String startTime = LocalDate.now().minusYears(1).format(DateTimeFormatter.ofPattern("yyyy-MM"));
            String endTime = LocalDate.now().minusMonths(1).format(DateTimeFormatter.ofPattern("yyyy-MM"));
            if(startTime.compareTo("2019-09")<0){
                homeRequestEntity.setEndTime("2020-08");
                homeRequestEntity.setStartTime("2019-09");
            }else{
                homeRequestEntity.setEndTime(endTime);
                homeRequestEntity.setStartTime(startTime);
            }
        }
        List<HomeSourceMappingEntity> homeSourceMappingEntities = sourceClassificationDao.coverageRate(homeRequestEntity);
        //获取最近一年月份列表
        List<String> montListByTime = DateUtil.getMontListByTime(homeRequestEntity.getStartTime(), homeRequestEntity.getEndTime());
        List<Map<String, Object>> monthlistMap = new ArrayList<>();
        for (int i = 0; i < montListByTime.size(); i++) {
            Map mapTime = new HashMap();
            mapTime.put(montListByTime.get(i), 0);
            monthlistMap.add(mapTime);
        }
        List<Integer> coverageTotal = new ArrayList<>();
        List<String> yearArray = new ArrayList<>();
        //总主体数
        for (Map<String, Object> map : monthlistMap) {
            String key = map.keySet().iterator().next();
            String lastYearByNow = LocalDate.parse(key+"-01").minusYears(1).format(DateTimeFormatter.ofPattern("yyyy"));//根据当期那时间获取上一年时间节点
            Integer totaCount = total(homeRequestEntity.getAreaCode(),lastYearByNow);
            for (HomeSourceMappingEntity homeSourceMappingEntity : homeSourceMappingEntities) {
                String time = homeSourceMappingEntity.getYEAR();
                if (time.equals(key)) {
                    int  total=0;
                    //计算总覆盖率
                    if (totaCount!=0) {
                        total= new BigDecimal(homeSourceMappingEntity.getTotalStart()).divide(new BigDecimal(totaCount), 2, BigDecimal.ROUND_HALF_DOWN).multiply(new BigDecimal(100)).intValue();
                    }
                    map.put(key, total);
                    break;
                }
            }
            coverageTotal.add(Integer.parseInt(map.get(key).toString()));
            yearArray.add(key);
        }
        SourceResponseEnetity sourceResponseEnetity = new SourceResponseEnetity();
        sourceResponseEnetity.setCoverageTotalArray(coverageTotal);
        sourceResponseEnetity.setYearArray(yearArray);
        return sourceResponseEnetity;
    }
    /**
     *@参数
     *@返回值
     * 处理跨年取年指标
     */
    private Integer total(String areaCode,String time) {
        HomeRequestEntity homeRequestEntity = new HomeRequestEntity();
        //截取年份
        //设置年指标查询条件
        homeRequestEntity.setTime(time);
        homeRequestEntity.setAreaCode(areaCode);
        HomeSourceMappingEntity yearIndex = sourceClassificationDao.getGisAnalysisVy(homeRequestEntity);
        if (yearIndex != null) {
            return yearIndex.getTotalCount()!=null?(yearIndex.getTotalCount().intValue()):0;
        }else{
            return 0;
        }
    }
    @Override
    public SourceResponseEnetity getCoverageSituation(HomeRequestEntity homeRequestEntity) {
        HandleTimeGis(homeRequestEntity);
        HomeSourceMappingEntity coverageSituation = sourceClassificationDao.getCoverageSituation(homeRequestEntity);
        HomeSourceMappingEntity coverageSituationVy = sourceClassificationDao.getCoverageSituationVy(homeRequestEntity);
        int communituRate=0;
        int companyRate =0;
        int institutionalUnitRate =0;
        if(null!=coverageSituationVy&&null!=coverageSituation){
            //小区覆盖率
            if (coverageSituationVy.getCommunityCount()!=null&&coverageSituationVy.getCommunityCount().intValue() !=0) {
                communituRate = new BigDecimal(coverageSituation.getCommunityStart()!=null?coverageSituation.getCommunityStart():0).divide(new BigDecimal(coverageSituationVy.getCommunityCount()), 2, BigDecimal.ROUND_HALF_DOWN).multiply(new BigDecimal(100)).intValue();
            }
            //企业覆盖率
            if (coverageSituationVy.getCompanyCount()!=null&&coverageSituationVy.getCompanyCount().intValue()!=0) {
                companyRate = new BigDecimal(coverageSituation.getCompanyStart()!=null?coverageSituation.getCompanyStart():0).divide(new BigDecimal(coverageSituationVy.getCompanyCount()), 2, BigDecimal.ROUND_HALF_DOWN).multiply(new BigDecimal(100)).intValue();
            }
            //公共机构覆盖率
            if (coverageSituationVy.getCompanyCount()!=null&&coverageSituationVy.getCompanyCount().intValue()!=0) {
                institutionalUnitRate = new BigDecimal(coverageSituation.getInstitutionalUnitStart()!=null?coverageSituation.getInstitutionalUnitStart():0).divide(new BigDecimal(coverageSituationVy.getInstitutionalUnitCount()), 2, BigDecimal.ROUND_HALF_DOWN).multiply(new BigDecimal(100)).intValue();
            }
            SourceResponseEnetity sourceResponseEnetity = new SourceResponseEnetity();
            sourceResponseEnetity.setCommunity(communituRate);
            sourceResponseEnetity.setCommunityStart(GetIntUtil.getInt(coverageSituation.getCommunityStart()!=null?coverageSituation.getCommunityStart():0));
            sourceResponseEnetity.setCommunityCount(GetIntUtil.getInt(coverageSituationVy.getCommunityCount()!=null?coverageSituationVy.getCommunityCount():0));
            sourceResponseEnetity.setCompany(companyRate);
            sourceResponseEnetity.setCompanyStart(GetIntUtil.getInt(coverageSituation.getCompanyStart()!=null?coverageSituation.getCompanyStart():0));
            sourceResponseEnetity.setCompanyCount(GetIntUtil.getInt(coverageSituationVy.getCompanyCount()!=null?coverageSituationVy.getCompanyCount():0));
            sourceResponseEnetity.setInstitutionalUnit(institutionalUnitRate);
            sourceResponseEnetity.setInstitutionalUnitStart(GetIntUtil.getInt(coverageSituation.getInstitutionalUnitStart()!=null?coverageSituation.getInstitutionalUnitStart():0));
            sourceResponseEnetity.setInstitutionalUnitCount(GetIntUtil.getInt(coverageSituationVy.getInstitutionalUnitCount()!=null?coverageSituationVy.getInstitutionalUnitCount():0));
            return sourceResponseEnetity;
        }else{
            SourceResponseEnetity sourceResponseEnetity = new SourceResponseEnetity();
            sourceResponseEnetity.setCommunity(0);
            sourceResponseEnetity.setCommunityStart(0);
            sourceResponseEnetity.setCommunityCount(0);
            sourceResponseEnetity.setCompany(0);
            sourceResponseEnetity.setCompanyStart(0);
            sourceResponseEnetity.setCompanyCount(0);
            sourceResponseEnetity.setInstitutionalUnit(0);
            sourceResponseEnetity.setInstitutionalUnitStart(0);
            sourceResponseEnetity.setInstitutionalUnitCount(0);
            return sourceResponseEnetity;
        }
    }

    @Override
    public List<SourceResponseEnetity> getAreaCoverageRanking(HomeRequestEntity homeRequestEntity) {
        if (StringUtils.isEmpty(homeRequestEntity.getTime()) ) {
            String mon = DateUtil.getOldMon();
            homeRequestEntity.setTime(mon);
            commonHandleTime(homeRequestEntity);
        }else {
            //处理时间条件
            commonHandleTime(homeRequestEntity);
        }
        List<SourceResponseEnetity> list = new ArrayList<>();
        List<HomeSourceMappingEntity> areaCoverageRanking = sourceClassificationDao.getAreaCoverageRankingNew(homeRequestEntity);
        if (areaCoverageRanking!=null) {
            for (HomeSourceMappingEntity homeSourceMappingEntity : areaCoverageRanking) {
                int communituRate = 33;
                int companyRate = 33;
                int institutionalUnitRate = 34;
                int complexCoverage =0;
                double community=homeSourceMappingEntity.getCommunityStart()!=null?homeSourceMappingEntity.getCommunityStart():0;
                double company=homeSourceMappingEntity.getCompanyStart()!=null?homeSourceMappingEntity.getCompanyStart():0;
                double institutionalUnit=homeSourceMappingEntity.getInstitutionalUnitStart()!=null?homeSourceMappingEntity.getInstitutionalUnitStart():0;
                double totalCount=homeSourceMappingEntity.getTotalCount()!=null?homeSourceMappingEntity.getTotalCount():0;
                double total=community+company+institutionalUnit;
                if(total!=0.0){
                    //小区覆盖率占比
                    communituRate = new BigDecimal(community).divide(new BigDecimal(total), 3, BigDecimal.ROUND_HALF_DOWN).multiply(new BigDecimal(1000)).intValue();
                    int communitus=communituRate%10;
                    //企业覆盖率占比
                    companyRate = new BigDecimal(company).divide(new BigDecimal(total), 3, BigDecimal.ROUND_HALF_DOWN).multiply(new BigDecimal(1000)).intValue();
                    int companys=companyRate%10;
                    //公共机构覆盖率占比
                    institutionalUnitRate = new BigDecimal(institutionalUnit).divide(new BigDecimal(total), 3, BigDecimal.ROUND_HALF_DOWN).multiply(new BigDecimal(1000)).intValue();
                    int institutionalUnits=institutionalUnitRate%10;
                    companyRate=Math.round(companyRate/10.0f);
                    communituRate=Math.round(communituRate/10.0f);
                    institutionalUnitRate=Math.round(institutionalUnitRate/10.0f);
                    if(companyRate+communituRate+institutionalUnitRate==99){
                            if(communitus>=companys){
                               if(communitus>=institutionalUnits) {
                                   communituRate=communituRate+1;
                               }else{
                                   institutionalUnitRate=institutionalUnitRate+1;
                               }
                            }else{
                                if(companys>=institutionalUnits) {
                                    companyRate=companyRate+1;
                                }else{
                                    institutionalUnitRate=institutionalUnitRate+1;
                                }
                            }
                    }else if(companyRate+communituRate+institutionalUnitRate==101){
                        if(communitus<=companys){
                            if(communitus<=institutionalUnits) {
                                communituRate=communituRate-1;
                            }else {
                                institutionalUnitRate=institutionalUnitRate-1;
                            }
                        }else{
                            if(companys<=institutionalUnits) {
                                companyRate=companyRate-1;
                            }else {
                                institutionalUnitRate=institutionalUnitRate-1;
                            }
                        }
                    }
                }
                //综合覆盖率
                if(totalCount!=0.0){
                    complexCoverage = new BigDecimal(total).divide(new BigDecimal(totalCount),3,BigDecimal.ROUND_HALF_DOWN).multiply(new BigDecimal(1000)).intValue();
                    complexCoverage=Math.round(complexCoverage/10.0f);
                }
                SourceResponseEnetity sourceResponseEnetity = new SourceResponseEnetity();
                sourceResponseEnetity.setAreaName(homeSourceMappingEntity.getAreaName());
                sourceResponseEnetity.setCommunity(communituRate);
                sourceResponseEnetity.setCompany(companyRate);
                sourceResponseEnetity.setPublicInstitutions(institutionalUnitRate);
                sourceResponseEnetity.setComplexCoverage(complexCoverage);
                list.add(sourceResponseEnetity);
            }
        }
        return list;
    }
    @Override
    public SourceResponseEnetity getBuildingProgress(HomeRequestEntity homeRequestEntity) {
        if (StringUtils.isEmpty(homeRequestEntity.getTime()) ) {
            String mon = DateUtil.getOldMon();
            homeRequestEntity.setTime(mon);
        }
        HomeSourceMappingEntity buildingProgress = sourceClassificationDao.getBuildingProgressNew(homeRequestEntity);
        SourceResponseEnetity sourceResponseEnetity = new SourceResponseEnetity();
        if(null==buildingProgress){
            sourceResponseEnetity.setDemonstrationCommunityBefore(0);
            sourceResponseEnetity.setDemonstrationCommunitystart(0);
            sourceResponseEnetity.setDemonstrationCommunityComplete(0);
            sourceResponseEnetity.setDemonstrationCommunityTotal(0);
            sourceResponseEnetity.setDemonstrationSliceBefore(0);
            sourceResponseEnetity.setDemonstrationSliceStart(0);
            sourceResponseEnetity.setDemonstrationSliceComplete(0);
            sourceResponseEnetity.setDemonstrationSliceTotal(0);
            sourceResponseEnetity.setBusinessBefore(0);
            sourceResponseEnetity.setBusinessStart(0);
            sourceResponseEnetity.setBusinessComplete(0);
            sourceResponseEnetity.setBusinessTotal(0);
            sourceResponseEnetity.setCommunityBefore(0);
            sourceResponseEnetity.setCommunityStartt(0);
            sourceResponseEnetity.setCommunityComplete(0);
            sourceResponseEnetity.setCommunityTotall(0);
        }else{
            sourceResponseEnetity.setDemonstrationCommunityBefore(GetIntUtil.getInt(buildingProgress.getDemonstrationCommunityBefore()!=null?buildingProgress.getDemonstrationCommunityBefore():0));
            sourceResponseEnetity.setDemonstrationCommunitystart(GetIntUtil.getInt(buildingProgress.getDemonstrationCommunitystart()!=null?buildingProgress.getDemonstrationCommunitystart():0));
            sourceResponseEnetity.setDemonstrationCommunityComplete(GetIntUtil.getInt(buildingProgress.getDemonstrationCommunityComplete()!=null?buildingProgress.getDemonstrationCommunityComplete():0));
            sourceResponseEnetity.setDemonstrationCommunityTotal(GetIntUtil.getInt(buildingProgress.getDemonstrationCommunityTotal()!=null?buildingProgress.getDemonstrationCommunityTotal():0));
            sourceResponseEnetity.setDemonstrationSliceBefore(GetIntUtil.getInt(buildingProgress.getDemonstrationSliceBefore()!=null?buildingProgress.getDemonstrationSliceBefore():0));
            sourceResponseEnetity.setDemonstrationSliceStart(GetIntUtil.getInt(buildingProgress.getDemonstrationSliceStart()!=null?buildingProgress.getDemonstrationSliceStart():0));
            sourceResponseEnetity.setDemonstrationSliceComplete(GetIntUtil.getInt(buildingProgress.getDemonstrationSliceComplete()!=null?buildingProgress.getDemonstrationSliceComplete():0));
            sourceResponseEnetity.setDemonstrationSliceTotal(GetIntUtil.getInt(buildingProgress.getDemonstrationSliceTotal()!=null?buildingProgress.getDemonstrationSliceTotal():0));
            sourceResponseEnetity.setBusinessBefore(GetIntUtil.getInt(buildingProgress.getBusinessBefore()!=null?buildingProgress.getBusinessBefore():0));
            sourceResponseEnetity.setBusinessStart(GetIntUtil.getInt(buildingProgress.getBusinessStart()!=null?buildingProgress.getBusinessStart():0));
            sourceResponseEnetity.setBusinessComplete(GetIntUtil.getInt(buildingProgress.getBusinessComplete()!=null?buildingProgress.getBusinessComplete():0));
            sourceResponseEnetity.setBusinessTotal(GetIntUtil.getInt(buildingProgress.getBusinessTotal()!=null?buildingProgress.getBusinessTotal():0));
            sourceResponseEnetity.setCommunityBefore(GetIntUtil.getInt(buildingProgress.getCommunityBefore()!=null?buildingProgress.getCommunityBefore():0));
            sourceResponseEnetity.setCommunityStartt(GetIntUtil.getInt(buildingProgress.getCommunityStartt()!=null?buildingProgress.getCommunityStartt():0));
            sourceResponseEnetity.setCommunityComplete(GetIntUtil.getInt(buildingProgress.getCommunityComplete()!=null?buildingProgress.getCommunityComplete():0));
            sourceResponseEnetity.setCommunityTotall(GetIntUtil.getInt(buildingProgress.getCommunityTotall()!=null?buildingProgress.getCommunityTotall():0));
        }
        return sourceResponseEnetity;
    }

    @Override
    public  Map<String, Object> getTimedDemonstration(HomeRequestEntity homeRequestEntity) {
        List<HomeSourceMappingEntity> timedDemonstration = sourceClassificationDao.getTimedDemonstration(homeRequestEntity);
        List<String> community = new ArrayList<>();
        List<String> business = new ArrayList<>();
        for (int i = 0; i < timedDemonstration.size(); i++) {
            if (i == 3) {
                break;
            }
            community.add(timedDemonstration.get(i).getCommunityArea()!=null?timedDemonstration.get(i).getCommunityArea():"");
            business.add(timedDemonstration.get(i).getBusiness()!=null?timedDemonstration.get(i).getBusiness():"");
        }
        Map<String, Object> map = new HashMap<>();
        map.put("timedDemonstrationCommunityList", community);
        map.put("timedDemonstrationBusinessList", business);
        return map;
    }

    @Override
    public  List<SourceResponseEnetity> getDemonstrationPointBuildDetain(HomeRequestEntity homeRequestEntity) {
        if (StringUtils.isEmpty(homeRequestEntity.getTime()) ) {
            String mon = DateUtil.getOldMon();
            homeRequestEntity.setTime(mon);
            commonHandleTime(homeRequestEntity);
        }else {
            commonHandleTime(homeRequestEntity);
        }
        List<HomeSourceMappingEntity> demonstrationPointBuildDetain = sourceClassificationDao.getDemonstrationPointBuildDetainNew(homeRequestEntity);
        List<SourceResponseEnetity> list = new ArrayList<>();
        for (HomeSourceMappingEntity homeSourceMappingEntity : demonstrationPointBuildDetain) {
            int demonstrationCommunityRadio=25;
            int demonstrationSliceRadio=25;
            int businessRadio=25;
            int communityRadio=25;
            int complexCoverage=0;
            double demonstrationCommunity=homeSourceMappingEntity.getDemonstrationCommunityComplete()!=null?homeSourceMappingEntity.getDemonstrationCommunityComplete():0;
            double demonstrationSlice=homeSourceMappingEntity.getDemonstrationSliceComplete()!=null?homeSourceMappingEntity.getDemonstrationSliceComplete():0;
            double business=homeSourceMappingEntity.getBusinessComplete()!=null?homeSourceMappingEntity.getBusinessComplete():0;
            double community=homeSourceMappingEntity.getCommunityComplete()!=null?homeSourceMappingEntity.getCommunityComplete():0;
            double totalCount=homeSourceMappingEntity.getTotalCount()!=null?homeSourceMappingEntity.getTotalCount():0;
            double total=community+demonstrationCommunity+demonstrationSlice+business;
            SourceResponseEnetity sourceResponseEnetity = new SourceResponseEnetity();
            if(total!=0.0){
                //示范小区占比
                int demonstrationCommunityRadioInt = new BigDecimal(demonstrationCommunity).divide(new BigDecimal(total), 3,BigDecimal.ROUND_HALF_DOWN).multiply(new BigDecimal(1000)).intValue();
                //示范片区占比
                int demonstrationSliceRadioInt = new BigDecimal(demonstrationSlice).divide(new BigDecimal(total), 3,BigDecimal.ROUND_HALF_DOWN).multiply(new BigDecimal(1000)).intValue();
                //定时定点商业区占比
                int businessRadioInt = new BigDecimal(business).divide(new BigDecimal(total), 3,BigDecimal.ROUND_HALF_DOWN).multiply(new BigDecimal(1000)).intValue();
                //定时定点小区占比
                int communityRadioInt = new BigDecimal(community).divide(new BigDecimal(total), 3,BigDecimal.ROUND_HALF_DOWN).multiply(new BigDecimal(1000)).intValue();
                demonstrationCommunityRadio=demonstrationCommunityRadioInt/10;
                demonstrationSliceRadio=demonstrationSliceRadioInt/10;
                businessRadio=businessRadioInt/10;
                communityRadio=communityRadioInt/10;
                if(demonstrationCommunityRadio+demonstrationSliceRadio+businessRadio+communityRadio==99){
                    if(demonstrationCommunityRadioInt>=demonstrationSliceRadioInt&&demonstrationCommunityRadioInt>=businessRadioInt&&demonstrationCommunityRadioInt>=communityRadioInt){
                        demonstrationCommunityRadio=demonstrationCommunityRadio+1;
                    }else
                    if(demonstrationSliceRadioInt>=demonstrationCommunityRadioInt&&demonstrationSliceRadioInt>=businessRadioInt&&demonstrationSliceRadioInt>=communityRadioInt){
                        demonstrationSliceRadio=demonstrationSliceRadio+1;
                    }else
                    if(businessRadioInt>=demonstrationCommunityRadioInt&&businessRadioInt>=demonstrationSliceRadioInt&&businessRadioInt>=communityRadioInt){
                        businessRadio=businessRadio+1;
                    }else
                    if(communityRadioInt>=demonstrationCommunityRadioInt&&communityRadioInt>=demonstrationSliceRadioInt&&communityRadioInt>=businessRadioInt){
                        communityRadio=communityRadio+1;
                    }
                }else if(demonstrationCommunityRadio+demonstrationSliceRadio+businessRadio+communityRadio==98){
                    if(demonstrationCommunityRadioInt>=demonstrationSliceRadioInt&&demonstrationCommunityRadioInt>=businessRadioInt&&demonstrationCommunityRadioInt>=communityRadioInt){
                        demonstrationCommunityRadio=demonstrationCommunityRadio+1;
                        if(demonstrationSliceRadioInt>=businessRadioInt&&demonstrationSliceRadioInt>=communityRadioInt){
                            demonstrationSliceRadio=demonstrationSliceRadio+1;
                        }else
                        if(businessRadioInt>=demonstrationSliceRadioInt&&businessRadioInt>=communityRadioInt){
                            businessRadio=businessRadio+1;
                        }else
                        if(communityRadioInt>=demonstrationSliceRadioInt&&communityRadioInt>=businessRadioInt){
                            communityRadio=communityRadio+1;
                        }
                    }
                    if(demonstrationSliceRadioInt>=demonstrationCommunityRadioInt&&demonstrationSliceRadioInt>=businessRadioInt&&demonstrationSliceRadioInt>=communityRadioInt){
                        demonstrationSliceRadio=demonstrationSliceRadio+1;
                        if(demonstrationCommunityRadioInt>=businessRadioInt&&demonstrationCommunityRadioInt>=communityRadioInt){
                            demonstrationCommunityRadio=demonstrationCommunityRadio+1;
                        }else
                        if(businessRadioInt>=demonstrationCommunityRadioInt&&businessRadioInt>=communityRadioInt){
                            businessRadio=businessRadio+1;
                        }else
                        if(communityRadioInt>=demonstrationCommunityRadioInt&&communityRadioInt>=businessRadioInt){
                            communityRadio=communityRadio+1;
                        }
                    }
                    if(businessRadioInt>=demonstrationCommunityRadioInt&&businessRadioInt>=demonstrationSliceRadioInt&&businessRadioInt>=communityRadioInt){
                        businessRadio=businessRadio+1;
                        if(demonstrationCommunityRadioInt>=demonstrationSliceRadioInt&&demonstrationCommunityRadioInt>=communityRadioInt){
                            demonstrationCommunityRadio=demonstrationCommunityRadio+1;
                        }else
                        if(demonstrationSliceRadioInt>=demonstrationCommunityRadioInt&&demonstrationSliceRadioInt>=communityRadioInt){
                            demonstrationSliceRadio=demonstrationSliceRadio+1;
                        }else
                        if(communityRadioInt>=demonstrationCommunityRadioInt&&communityRadioInt>=demonstrationSliceRadioInt){
                            communityRadio=communityRadio+1;
                        }
                    }
                    if(communityRadioInt>=demonstrationCommunityRadioInt&&communityRadioInt>=demonstrationSliceRadioInt&&communityRadioInt>=businessRadioInt){
                        communityRadio=communityRadio+1;
                        if(demonstrationCommunityRadioInt>=demonstrationSliceRadioInt&&demonstrationCommunityRadioInt>=businessRadioInt){
                            demonstrationCommunityRadio=demonstrationCommunityRadio+1;
                        }else
                        if(demonstrationSliceRadioInt>=demonstrationCommunityRadioInt&&demonstrationSliceRadioInt>=businessRadioInt){
                            demonstrationSliceRadio=demonstrationSliceRadio+1;
                        }else
                        if(businessRadioInt>=demonstrationCommunityRadioInt&&businessRadioInt>=demonstrationSliceRadioInt){
                            businessRadio=businessRadio+1;
                        }
                    }
                }else if(demonstrationCommunityRadio+demonstrationSliceRadio+businessRadio+communityRadio==97){
                    if(demonstrationCommunityRadioInt<=demonstrationSliceRadioInt&&demonstrationCommunityRadioInt<=businessRadioInt&&demonstrationCommunityRadioInt<=communityRadioInt){
                        demonstrationSliceRadio=demonstrationSliceRadio+1;
                        businessRadio=businessRadio+1;
                        communityRadio=communityRadio+1;
                    }else
                    if(demonstrationSliceRadioInt<=demonstrationCommunityRadioInt&&demonstrationSliceRadioInt<=businessRadioInt&&demonstrationSliceRadioInt<=communityRadioInt){
                        demonstrationCommunityRadio=demonstrationCommunityRadio+1;
                        businessRadio=businessRadio+1;
                        communityRadio=communityRadio+1;
                    }else
                    if(businessRadioInt<=demonstrationCommunityRadioInt&&businessRadioInt<=demonstrationSliceRadioInt&&businessRadioInt<=communityRadioInt){
                        demonstrationCommunityRadio=demonstrationCommunityRadio+1;
                        demonstrationSliceRadio=demonstrationSliceRadio+1;
                        communityRadio=communityRadio+1;
                    }else
                    if(communityRadioInt<=demonstrationCommunityRadioInt&&communityRadioInt<=demonstrationSliceRadioInt&&communityRadioInt<=businessRadioInt){
                        demonstrationCommunityRadio=demonstrationCommunityRadio+1;
                        demonstrationSliceRadio=demonstrationSliceRadio+1;
                        businessRadio=businessRadio+1;
                    }
                }
            }
            sourceResponseEnetity.setAreaName(homeSourceMappingEntity.getAreaName());
            sourceResponseEnetity.setDemonstrationCommunityRadio(demonstrationCommunityRadio);
            sourceResponseEnetity.setDemonstrationSliceRadio(demonstrationSliceRadio);
            sourceResponseEnetity.setBusinessRadio(businessRadio);
            sourceResponseEnetity.setCommunityRadio(communityRadio);
            //综合覆盖率
            if(totalCount!=0.0){
                complexCoverage = new BigDecimal(total).divide(new BigDecimal(totalCount),3,BigDecimal.ROUND_HALF_DOWN).multiply(new BigDecimal(1000)).intValue();
                complexCoverage=Math.round(complexCoverage/10.0f);
            }
            sourceResponseEnetity.setCoverageTotal(complexCoverage);
            list.add(sourceResponseEnetity);
        }
        return list;
    }

    @Override
    public SourceResponseEnetity getGisAnalysis(HomeRequestEntity homeRequestEntity) {
        HandleTimeGis(homeRequestEntity);
        HomeSourceMappingEntity gisAnalysis = sourceClassificationDao.getGisAnalysis(homeRequestEntity);
        HomeSourceMappingEntity gisAnalysisVy = sourceClassificationDao.getGisAnalysisVy(homeRequestEntity);
        int city =0;
        int country =0;
        int total =0;
        //计算设区市覆盖率
        if (gisAnalysis!=null && gisAnalysis.getCoverageCity()!=null &&gisAnalysis.getCoverageCity().intValue()!=0) {
            city = new BigDecimal(gisAnalysis.getCoverageCityCount()).divide(new BigDecimal(gisAnalysis.getCoverageCity()), 2, BigDecimal.ROUND_HALF_DOWN).multiply(new BigDecimal(100)).intValue();
        }
        //计算设县市覆盖率
        if (gisAnalysis!=null && gisAnalysis.getCoverageCountry()!=null &&gisAnalysis.getCoverageCountry().intValue()!=0) {
            country = new BigDecimal(gisAnalysis.getCoverageCountryCount()).divide(new BigDecimal(gisAnalysis.getCoverageCountry()), 2, BigDecimal.ROUND_HALF_DOWN).multiply(new BigDecimal(100)).intValue();
        }
        //总覆盖率
        if (gisAnalysis!=null &&gisAnalysisVy!=null && gisAnalysisVy.getTotalCount()!=null && gisAnalysisVy.getTotalCount().intValue()!=0) {
            total = new BigDecimal(gisAnalysis.getTotalStart()).divide(new BigDecimal(gisAnalysisVy.getTotalCount()), 2, BigDecimal.ROUND_HALF_DOWN).multiply(new BigDecimal(100)).intValue();
        }
        SourceResponseEnetity sourceResponseEnetity = new SourceResponseEnetity();
        sourceResponseEnetity.setCoverageCountry(country);
        sourceResponseEnetity.setCoverageCity(city);
        sourceResponseEnetity.setCoverageTotal(total);
        if (gisAnalysis!=null) {
            sourceResponseEnetity.setGarbageClassificationSubjectCount(GetIntUtil.getInt( gisAnalysis.getTotalStart()!=null?gisAnalysis.getTotalStart():0));
        }else{
            sourceResponseEnetity.setGarbageClassificationSubjectCount(0);
        }
        return sourceResponseEnetity;
    }

    /**
     *开展情况对比展示
     */
    @Override
    public List<SourceResponseEnetity> getContrastAnalysis(HomeRequestEntity homeRequestEntity) {
        if (StringUtils.isEmpty(homeRequestEntity.getTime()) ) {
            String mon = DateUtil.getOldMon();
            homeRequestEntity.setTime(mon);
            commonHandleTime(homeRequestEntity);
        }else {
            commonHandleTime(homeRequestEntity);
        }

        List<SourceResponseEnetity> list = new ArrayList<>();
        List<HomeSourceMappingEntity> areaCoverageRanking = sourceClassificationDao.getContrastRanking(homeRequestEntity);
        if (areaCoverageRanking!=null) {
            for (HomeSourceMappingEntity homeSourceMappingEntity : areaCoverageRanking) {
                int communityStart=homeSourceMappingEntity.getCommunityStart()!=null?homeSourceMappingEntity.getCommunityStart().intValue():0;
                int communityCount= homeSourceMappingEntity.getCommunityCount()!=null?homeSourceMappingEntity.getCommunityCount().intValue():0;
                SourceResponseEnetity sourceResponseEnetity = new SourceResponseEnetity();
                sourceResponseEnetity.setAreaName(homeSourceMappingEntity.getAreaName());
                sourceResponseEnetity.setCommunityStart(communityStart);
                sourceResponseEnetity.setCommunityCount(communityCount);
                list.add(sourceResponseEnetity);
            }
        }
        return list;
    }
    /**
     *@参数
     *@返回值
     * 处理时间条件
     */
    private void commonHandleTime(HomeRequestEntity homeRequestEntity){
        //处理时间条件
        String yyyy = LocalDate.now().format(DateTimeFormatter.ofPattern("yyyy"));
        if (homeRequestEntity.getTime().substring(0,4).equals(yyyy)) {
            String substring = LocalDate.now().minusYears(1).format(DateTimeFormatter.ofPattern("yyyyy"));
            homeRequestEntity.setStartTime(substring);
        }else {
            String lastYear = LocalDate.parse(homeRequestEntity.getTime()+"-01").minusYears(1).format(DateTimeFormatter.ofPattern("yyyy"));
            homeRequestEntity.setStartTime(lastYear);
        }
    }
    /**
     *@参数
     *@返回值
     * 处理Gis时间
     */
    private void HandleTimeGis(HomeRequestEntity homeRequestEntity){
        //如果是年初取上上年数据，否则取上年
        String nowTime = LocalDate.now().format(DateTimeFormatter.ofPattern("yyyy-MM"));
        String initialTime = LocalDate.now().format(DateTimeFormatter.ofPattern("yyyy"))+"-01";
        if (nowTime.equals(initialTime)) {
            String year = LocalDate.now().minusYears(2).format(DateTimeFormatter.ofPattern("yyyy"));
            homeRequestEntity.setTime(year);
        }else {
            String year = LocalDate.now().minusYears(1).format(DateTimeFormatter.ofPattern("yyyy"));
            homeRequestEntity.setTime(year);
        }
    }
}
